package com.liang.auth.application.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.liang.auth.application.convert.AuthPermissionDTOConverter;
import com.liang.auth.application.dto.AuthPermissionDTO;
import com.liang.auth.api.entity.Result;
import com.liang.auth.domain.bo.AuthPermissionBO;
import com.liang.auth.domain.service.AuthPermissionDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
public class PermissionController {

    private final AuthPermissionDomainService permissionDomainService;

    /**
     * 新增权限
     */
    @PostMapping("/save")
    public Result<Void> save(@RequestBody AuthPermissionDTO authPermissionDTO) {
        log.info("PermissionController.save.dto：{}", JSONUtil.toJsonStr(authPermissionDTO));
        try {
            Preconditions.checkNotNull(authPermissionDTO.getName(), "权限名称不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTO2BO(authPermissionDTO);
            permissionDomainService.save(authPermissionBO);
            return Result.ok("新增权限成功");
        } catch (Exception e) {
            log.error("PermissionController.save.error：{}", e.getMessage(), e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     * 修改权限
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        log.info("PermissionController.update.dto：{}", JSONUtil.toJsonStr(authPermissionDTO));
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTO2BO(authPermissionDTO);
            permissionDomainService.update(authPermissionBO);
            return Result.ok("修改权限成功");
        } catch (Exception e) {
            log.error("PermissionController.update.error：{}", e.getMessage(), e);
            return Result.fail("修改权限失败");
        }
    }

    /**
     * 启用/禁用权限
     */
    @PostMapping("/changeStatus")
    public Result<Void> updateStatus(@RequestBody AuthPermissionDTO authPermissionDTO) {
        log.info("PermissionController.updateStatus.dto：{}", JSONUtil.toJsonStr(authPermissionDTO));
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            Integer status = authPermissionDTO.getStatus();
            Preconditions.checkNotNull(status, "权限状态不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTO2BO(authPermissionDTO);
            permissionDomainService.update(authPermissionBO);
            return status == 0 ? Result.ok("禁用权限成功") : Result.ok("启用权限成功");
        } catch (Exception e) {
            log.error("PermissionController.update.error：{}", e.getMessage(), e);
            return Result.fail("修改权限失败");
        }
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        log.info("PermissionController.delete.dto：{}", JSONUtil.toJsonStr(authPermissionDTO));
        try {
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限ID不能为空");
            permissionDomainService.delete(authPermissionDTO.getId());
            return Result.ok("删除权限成功");
        } catch (Exception e) {
            log.error("PermissionController.delete.error：{}", e.getMessage(), e);
            return Result.fail("删除权限失败");
        }
    }

    /**
     * 查询用户权限
     */
    @GetMapping("/list")
    public Result<List<AuthPermissionDTO>> list(@RequestParam("userId") Long userId) {
        log.info("PermissionController.list.userId：{}", userId);
        return Result.ok(null);
//        return Result.ok(permissionDomainService.list(userId));
    }

    /**
     * 查询用户权限
     */
    @RequestMapping("/getPermission")
    public Result<List<String>> getPermission(String username) {
        try {
            log.info("PermissionController.getPermission.userName:{}",username);
            Preconditions.checkArgument(!StringUtils.isBlank(username), "用户id不能为空");
            return Result.ok(permissionDomainService.getPermission(username));
        } catch (Exception e) {
            log.error("PermissionController.getPermission.error:{}", e.getMessage(), e);
            return Result.fail("查询用户权限信息失败");
        }
    }
}
